<?php
/**
 * Created by JetBrains PhpStorm.
 * User: evan
 * Date: 13-3-5
 * Time: 下午3:27
 * To change this template use File | Settings | File Templates.
 */
class Staff extends MY_Agent_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $where = array('status'=>1, 'aid'=>$this->uInfo['aid']);
        $realname = $this->input->get_post('realname');

        $realname && $where['realname'] = $realname;

        $this->load->model('agent/Model_agent_staff', 'staff');
        $totalNum = $this->staff->count($where);
        $staffInfo = $this->staff->lists($limit, $offset, $where);

        $this->load->library('pagination');
        $config['total_rows'] = $totalNum;
        $config['per_page'] = $limit;
        $config['base_url'] = '/Staff/index';
        
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();

        $data = array(
            'staff' => $staffInfo,
            'page_html' => $pageHtml,
            'total_num' => $totalNum,
        );
        $this->load->view('agent/staff/index', $data);
    }

    public function create()
    {
        $this->load->view('agent/staff/create');
    }

    public function save()
    {
        $realname = $this->input->get_post('realname');
        $mobile = $this->input->get_post('mobile');
        $email = $this->input->get_post('email');

        $data = array(
        	'aid' => $this->uInfo['aid'],
            'realname' => $realname,
            'mobile' => $mobile,
            'email' => $email,
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->load->model('agent/Model_agent_staff', 'staff');
        $lastId = $this->staff->save($data);

        $this->load->helper('url');
        redirect(url('agent').'staff/index', 'refresh');
    }

    public function delete()
    {

        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $id     = (int)$this->input->get_post('id');
            $status = (int)$this->input->get_post('status');

            if (empty ($id)) {
                $response = error(100000);//参数不全
                break;
            }

            $this->load->model('agent/Model_agent_staff', 'staff');
            $item = $this->staff->getById($id);
            if (empty ($item))
            {
                $response = error(600002);
                break;
            }


            $rs = $this->staff->save(array('status' => $status), $id);
            
            if (!$rs) {
                $response = error(600003);
                break;
            }

        } while (false);

        $this->$this->json_output($response);
    }
}
